package xyz.xpman.prisonerfitness.activity;

import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.RequiresApi;

import org.jetbrains.annotations.NotNull;

import java.util.Objects;

import xyz.xpman.prisonerfitness.R;
import xyz.xpman.prisonerfitness.activity.base.BaseActivity;
import xyz.xpman.prisonerfitness.entity.User;
import xyz.xpman.prisonerfitness.service.UserService;
import xyz.xpman.prisonerfitness.util.StrKit;
import xyz.xpman.prisonerfitness.util.ToastUtil;

public class RegisterActivity extends BaseActivity {
    private UserService service;
    private EditText username, password, checkPassword;
    private RadioGroup sexGroup;



    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);

        service = UserService.ME;

        init();
    }

    //组件初始化方法
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void init() {
        getWindow().setNavigationBarColor(getResources().getColor(R.color.primary));

        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        checkPassword = findViewById(R.id.check_password);
        sexGroup = findViewById(R.id.sex);

        // 注册事件
        findViewById(R.id.register).setOnClickListener(view-> {
            if (R.id.register == view.getId()) {
                String message = register();

                if (StrKit.isBlank(message)) {
                    ToastUtil.showText(this, "注册成功!");

                    finish();
                    return;
                }

                ToastUtil.showText(this, message);
            }
        });

    }


    @RequiresApi(api = Build.VERSION_CODES.O)
    private String register() {
        String username = this.username.getText().toString();
        String password = this.password.getText().toString();
        String password2 = checkPassword.getText().toString();

        if (Objects.equals(password, password2)) {
            User user = new User();
            user.setUsername(username);
            user.setPassword(password2);

            String sex = getCheckedRadioText(sexGroup);
            if (StrKit.isBlank(sex)) {
                return "未选择角色!";
            }
            user.setSex(sex);

            return service.register(user);
        }
        return "两次密码输入不一致!";
    }

    private String getCheckedRadioText(@NotNull RadioGroup roleGroup) {
        RadioButton roleSelected = findViewById(roleGroup.getCheckedRadioButtonId());
        if (null == roleSelected) {
            return "";
        }
        return (String) roleSelected.getText();
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return processMenuItemForBack(item);
    }

}